package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/addressBook")
@RestController
public class AddressBookController {
    @Autowired
    AddressBookService addressBookService;

    //保存地址
    @PostMapping
    public R addressBook(@RequestBody AddressBook addressBook) {
        Long currentId = BaseContext.getCurrentId();

        addressBook.setUserId(currentId);
        boolean save = addressBookService.save(addressBook);

        return save ? R.success("保存成功") : R.error("保存失败");
    }

//    设置默认地址

    @PutMapping("/default")
    public R defaultAddress(@RequestBody AddressBook addressBook) {
        //将用户全部地址设置为0
        LambdaUpdateWrapper<AddressBook> up = new LambdaUpdateWrapper<>();
        up.set(AddressBook::getIsDefault, 0);
        up.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        addressBookService.update(up);
        //设置默认地址
        LambdaUpdateWrapper<AddressBook> up2 = new LambdaUpdateWrapper<>();
        up2.eq(AddressBook::getId, addressBook.getId());
        up2.set(AddressBook::getIsDefault, 1);
        boolean update = addressBookService.update(up2);
        return update ? R.success("默认地址修改成功") : R.error("默认地址修改失败");
    }

    //     查询指定用户的全部地址
    @GetMapping("/list")
    public R getAll() {
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<AddressBook> lqwq = new LambdaQueryWrapper<>();
        lqwq.eq(AddressBook::getUserId, currentId);
        List<AddressBook> list = addressBookService.list(lqwq);

       /* LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressBook::getId,id);
        List<AddressBook> list1 = addressBookService.list(lqw);*/
        return list == null ? R.error("该用户无地址") : R.success(list);
    }

    //  查询默认地址

    @GetMapping("/default")
    public R getDefault() {
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<AddressBook> lqwq = new LambdaQueryWrapper<>();
        lqwq.eq(AddressBook::getUserId, currentId);
        lqwq.eq(AddressBook::getIsDefault, 1);
        AddressBook one = addressBookService.getOne(lqwq);
        return R.success(one);
    }

    //根据ID查询地址
    @GetMapping("/{id}")
    public R getaddress(@PathVariable Long id) {
        //查寻地址
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressBook::getId, id);
        AddressBook one = addressBookService.getOne(lqw);
        return R.success(one);
    }



//    删除地址【可能是多个】
    @DeleteMapping
    public R delete(@RequestParam List<Long> ids){
        addressBookService.removeByIds(ids);
        return R.success("删除成功");
    }

//修改地址
    @PutMapping
    public  R update(@RequestBody AddressBook addressBook){
        //设置条件
        LambdaUpdateWrapper<AddressBook> lqwq = new LambdaUpdateWrapper<>();
        lqwq.eq(AddressBook::getId,addressBook.getId());
        //更新
        addressBookService.update(addressBook,lqwq);
        return R.success("修改成功");
    }

}
